<template>
    <el-dialog :title="title"
               :visible.sync="visible"
               :before-close="modalClose"
               :append-to-body="true"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               width="60%">
        <el-form :model="subData"
                 label-position="right"
                 label-width="150px"
                 ref="formModelRef"
                 :rules='formModelRules'>
            <el-form-item label="风险等级：" prop="rank">
                <el-select v-model="subData.rank"
                           class="w-300"
                           placeholder="请选择风险等级">
                    <el-option v-for="item in riskDataSources"
                               :key="item.id"
                               :label="item.name"
                               :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="条件列表：" class="is-required">
                <el-button type="primary"
                           @click="handleOption('addCondition',null, null)">添加
                </el-button>
            </el-form-item>
            <el-form-item>
                <element-table
                        :columns="columnsCondition"
                        :is-show-null="false"
                        :data="subData.conditions">
                    <template v-slot:score="{row,index}">
                        <el-select v-model="row.score"
                                   placeholder="请选择评分">
                            <el-option v-for="item in scoringDataSource"
                                       :key="item"
                                       :label="item"
                                       :value="item">
                            </el-option>
                        </el-select>
                    </template>

                    <template v-slot:conditionType="{row,index}">
                        <el-select v-model="row.conditionType"
                                   placeholder="请选择关系">
                            <el-option v-for="item in relationDataSources"
                                       :key="item.id"
                                       :label="item.name"
                                       :value="item.id">
                            </el-option>
                        </el-select>

                    </template>
                    <template v-slot:num="{row,index}">
                        <el-input v-model="row.num" placeholder="指征数量"></el-input>
                    </template>
                    <template v-slot:option="{row,index}">
                        <el-button type="danger"
                                   @click="handleOption('delCondition',index, row)">删除
                        </el-button>
                    </template>
                </element-table>

            </el-form-item>

            <el-form-item label="条件判断：" class="is-required">
                <el-button type="primary"
                           @click="handleOption('addJudgment',null, null)">添加
                </el-button>
            </el-form-item>

            <el-form-item>
                <element-table
                        :columns="columnsJudgment"
                        :is-show-null="false"
                        :data="subData.lines">

                    <template v-slot:leftConditionId="{row,index}">
                        <el-select v-model="row.leftConditionId"
                                   clearable
                                   placeholder="请选择条件">
                            <el-option v-for="item in subData.conditions"
                                       :key="item.conditionId"
                                       :label="'条件'+item.conditionId"
                                       :value="item.conditionId">
                            </el-option>
                        </el-select>

                    </template>
                    <template v-slot:relationType="{row,index}">
                        <el-select v-model="row.relationType"
                                   clearable
                                   placeholder="请选择关系">
                            <el-option v-for="item in testRelation"
                                       :key="item.id"
                                       :label="item.name"
                                       :value="item.id">
                            </el-option>
                        </el-select>
                    </template>
                    <template v-slot:rightConditionId="{row,index}">
                        <el-select v-model="row.rightConditionId"
                                   clearable
                                   placeholder="请选择条件">
                            <el-option v-for="item in subData.conditions"
                                       :key="item.conditionId"
                                       :label="'条件'+item.conditionId"
                                       :value="item.conditionId">
                            </el-option>
                        </el-select>

                    </template>

                    <template v-slot:option="{row,index}">
                        <el-button type="danger"
                                   @click="handleOption('delJudgment',index, row)">删除
                        </el-button>
                    </template>
                </element-table>

            </el-form-item>

            <el-form-item label="条件判断关系：">
                <el-select v-model="subData.column.leftLineId"
                           class="w-200"
                           placeholder="请选择条件判断">
                    <el-option v-for="item in subData.lines"
                               :key="item.lineId"
                               :label="'判断'+item.lineId"
                               :value="item.lineId">
                    </el-option>
                </el-select>
                <el-select v-model="subData.column.relationType"
                           class="w-100"
                           placeholder="请选择条件判断关系">
                    <el-option v-for="item in testRelation"
                               :key="item.id"
                               :label="item.name"
                               :value="item.id">
                    </el-option>
                </el-select>
                <el-select v-model="subData.column.rightLineId"
                           class="w-200"
                           placeholder="请选择条件判断">
                    <el-option v-for="item in subData.lines"
                               :key="item.lineId"
                               :label="'判断'+item.lineId"
                               :value="item.lineId">
                    </el-option>
                </el-select>

            </el-form-item>


        </el-form>
        <p align="center">
            <el-button @click="modalClose"
                       type="info">取 消
            </el-button>
            <el-button type="primary"
                       v-prevent-re-click
                       @click="handleOption('save',null, null)">保 存
            </el-button>
        </p>

    </el-dialog>

</template>

<script>
    import ElementTable from "../../../../base/ElementTable";

    export default {
        name: "dialogRiskAssessment",
        props: {
            dataObj: {
                type: Object,
                default: {}
            },
            visible: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                subData: {
                    rank: '',
                    conditions: [],
                    lines: [],
                    column: {
                        leftLineId: '',
                        rightLineId: '',
                        relationType: '',
                    },
                },
                testRelation: [{name: '且', id: 1}, {name: '或', id: 2}],
                scoringDataSource: [],
                riskDataSources: [
                    {
                        id: 1,
                        name: '低风险',
                    },
                    {
                        id: 2,
                        name: '中低风险',
                    },
                    {
                        id: 3,
                        name: '中风险',
                    },
                    {
                        id: 4,
                        name: '中高风险',
                    },
                    {
                        id: 5,
                        name: '高风险',
                    },
                    {
                        id: 6,
                        name: '超高风险',
                    }
                ],

                relationDataSources: [
                    {
                        id: 1,
                        name: '='
                    },
                    {
                        id: 2,
                        name: '>'
                    },
                    {
                        id: 3,
                        name: '>='
                    },
                    {
                        id: 4,
                        name: '<'
                    },
                    {
                        id: 5,
                        name: '<='
                    }
                ],
                title: '新 增',
                conditionNum: 0,
                judgmentNum: 0,
                columnsCondition: [
                    {
                        label: '条件序号',
                        prop: 'conditionId',
                        align: 'center',
                        formatter: (row, cellValue) => {
                            return '条件' + row.conditionId
                        },
                    }, {
                        label: 'count评分',
                        prop: 'score',
                        align: 'center',
                        isSlot: true
                    }, {
                        label: '关系',
                        prop: 'conditionType',
                        align: 'center',
                        isSlot: true
                    }, {
                        label: '指征数量',
                        prop: 'num',
                        align: 'center',
                        isSlot: true
                    }, {
                        label: '操作',
                        prop: 'option',
                        align: 'center',
                        isSlot: true
                    }
                ],
                columnsJudgment: [
                    {
                        label: '判断序号',
                        prop: 'lineId',
                        align: 'center',
                        formatter: (row, cellValue) => {
                            return '条件判断' + row.lineId
                        },
                    }, {
                        label: '条件',
                        prop: 'leftConditionId',
                        align: 'center',
                        isSlot: true
                    }, {
                        label: '关系',
                        prop: 'relationType',
                        align: 'center',
                        isSlot: true
                    }, {
                        label: '条件',
                        prop: 'rightConditionId',
                        align: 'center',
                        isSlot: true
                    }, {
                        label: '操作',
                        prop: 'option',
                        align: 'center',
                        isSlot: true
                    }
                ],
                formModelRules: {
                    rank: [{required: true, message: '请选择风险等级', trigger: 'change'},],
                }
            }
        },
        created() {
            if (this.dataObj.rank) {
                this.title = '编 辑'
                this.getBodyRiskDetail()
            } else {
                this.title = '新 增'
            }
            this.getScoreList()
        },
        methods: {
            modalClose() {
                this.$emit('update:visible', false);
            },
            //查询得分
            getScoreList() {
                this.post(this.api.healthMap.getScoreList).then((res) => {
                    if (res.data.code === '200') {
                        this.scoringDataSource = res.data.object
                    }
                })
            },
            //详情
            getBodyRiskDetail() {
                this.post(this.api.healthMap.getBodyRiskDetail, {
                    rank: this.dataObj.rank
                }).then((res) => {
                    if (res.data.code === '200') {
                        this.subData = Object.assign(this.subData,res.data.object)
                    }
                })
            },
            handleOption(op, index, row) {
                switch (op) {
                    case 'addCondition':
                        if (this.subData.conditions.length === 0) {
                            this.conditionNum = this.conditionNum + 1
                        } else {
                            this.conditionNum = Math.max.apply(null, this.subData.conditions.map(item => item.conditionId)) + 1
                        }
                        this.subData.conditions.push({
                            conditionId: this.conditionNum,
                            score: '',
                            conditionType: '',
                            num: '',
                        })
                        break;
                    case 'delCondition':
                        this.subData.conditions.splice(index, 1)
                        let currentConditionNumber = row.conditionId

                        this.subData.lines.forEach(y => {
                            if (y.leftConditionId === currentConditionNumber) {
                                y.leftConditionId = ''
                            }
                            if (y.rightConditionId === currentConditionNumber) {
                                y.rightConditionId = ''
                            }
                        })

                        break
                    case 'addJudgment':
                        if (this.subData.lines.length === 0) {
                            this.judgmentNum = this.judgmentNum + 1
                        } else {
                            this.judgmentNum = Math.max.apply(null, this.subData.lines.map(item => item.lineId)) + 1
                        }
                        this.subData.lines.push({
                            lineId: this.judgmentNum,
                            leftConditionId: '',
                            rightConditionId: '',
                            relationType: '',
                        })
                        break
                    case 'delJudgment':
                        this.subData.lines.splice(index, 1)
                        let currentJudgmentNumber = row.lineId
                        if (this.subData.column.rightLineId === currentJudgmentNumber) {
                            this.subData.column.rightLineId = ''
                        }
                        if (this.subData.column.leftLineId === currentJudgmentNumber) {
                            this.subData.column.leftLineId = ''
                        }
                        break
                    case 'save':
                        this.$refs.formModelRef.validate(valid => {
                            if (valid) {
                                if (this.subData.conditions.length === 0) {
                                    this.$message.warning('请添加条件')
                                } else if (this.subData.lines.length === 0) {
                                    this.$message.warning('请添加条件判断')
                                } else {
                                    this.post(this.api.healthMap.updateBodyRisk, this.subData).then((res) => {
                                        if (res.data.code === '200') {
                                            this.$message.success('更新成功!');
                                            this.$parent.getList();
                                            this.modalClose();
                                        }
                                    })
                                }
                            }
                        })

                        break

                }

            },
        },
        components: {
            'element-table': ElementTable
        }

    }
</script>

<style scoped>

</style>
